package com.qq.websocket;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServlet;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.qq.bean.Dynamics;
import com.qq.bean.FriendshipVO;

import com.qq.service.DynamicsService;
import com.qq.service.UserService;
import com.qq.service.UsersService;
import com.qq.service.impl.DynamicsServiceImpl;
import com.qq.service.impl.UsersServiceImpl;
@ServerEndpoint(value ="/insertdynamics/{account}")
public class insertDynamics extends HttpServlet{
	private static final Map<String,Session> dySessions = new ConcurrentHashMap<String, Session>();
	private static final Set<Session> mydysessions = Collections.synchronizedSet(new HashSet<>());//所有聊天管道
	private static final ObjectMapper omp = new ObjectMapper();
	private UsersService us = new UsersServiceImpl();
	private DynamicsService dysercice = new DynamicsServiceImpl();
	
	@OnOpen
    public void onOpen(Session session,@PathParam("account")String account) {
		
       System.out.println("连接创立");
       mydysessions.add(session);
       dySessions.put(account, session);
	   System.out.println("当前存在的用户和通道:"+dySessions+"1");
    }


    @OnClose
    public void onClose() {
    	System.out.println("连接关闭");
    	System.out.println(mydysessions+"关闭了"+"dy");
    	mydysessions.remove(mydysessions);
		Set<String> keys = dySessions.keySet();
		for(String key:keys) {
			if(dySessions.get(key) == mydysessions) {
				dySessions.remove(key);
			}
		}
    }
   
    public Session getSession(String acount) {
    	return dySessions.get(acount);
    }
  
    @OnMessage
    public void onMessage(String message,Session session) throws IOException {
    	System.out.println("接受信息"+message);
    	//1.把用户消息存入到数;据库
    	Dynamics dyn  = omp.readValue(message, Dynamics.class);
    	dyn.setDyTime(new Date());
    	dysercice.insertmydy(dyn);
    	System.out.println(dyn);
    	//查找好友
    	List<FriendshipVO> selectAllfriend = us.selectMyFriend(dyn.getAccounts());
    	Session friendSession = null;
    	for (FriendshipVO friendshipVO : selectAllfriend) {
    		friendSession = getSession(friendshipVO.getFriendaccount());
    		//把动态推送给好友
        	if(friendSession != null) {
        		dyMsg(friendSession, dyn);
        	}
		}
    	
    	//消息推送给自己，用于在页面回显示
    	dyMsg(session,dyn);
    }
    

    //推送消息给 用户
    public static void  dyMsg(Session session,Dynamics dynamics) throws JsonParseException, JsonMappingException, IOException {
	
		session.getBasicRemote().sendText(omp.writeValueAsString(dynamics));
		
	 } 



    
    @OnError
    public void onError(Throwable t) throws Throwable {
    	t.printStackTrace();
    	System.out.println("系统异常");
    }

}